/**
 * Sensor Network Development Kit for 1-Wire Network (SeN)
 * Version : 0.1
 * Revision date : 2008-10-13
 * Author: Dubravko Penezic, dpenezic@gmail.com
 * 
 */

package owLib;

import com.dalsemi.onewire.*;
import com.dalsemi.onewire.adapter.*;
import com.dalsemi.onewire.container.*;

/**
 * OWCommPath class implement methods for manage 1-wire path on 1-wire network to 1-wire device 
 * Version : 0.1.1
 * Revision date : 2008-10-18
 * 
 * @author Dubravko Penezic, dpenezic@gmail.com
 *
 */

public class OWCommPath
{
  // class variables
  private DSPortAdapter adapter;
  private OWDevice d; 
  private OWConf c; 
  private OWHub[] h;
 
  private boolean mPath = true;
  private String[] hAddress;
  private String[] hPorts;
  
/**
 * OWCommPath method set parameters of path to 1-wire device. If address of hub is set to "root" skip activating hubs on network.
 * 
 * @param adapter
 * @param d
 */
  public OWCommPath(DSPortAdapter adapter, OWDevice d)
  {
    
	  this.d = d;
	  this.hAddress = d.getHAddress();
	  this.hPorts = d.getHPorts();
	  this.h = new OWHub[this.hAddress.length];
	  this.adapter = adapter;
	    
	  if(this.hAddress[0].equalsIgnoreCase("root"))
		  mPath = false;

      if (c.debugFlag)
        System.out.println("Initializing the Path...");
      
      if(mPath){
    	  for(int i = 0; i < this.hAddress.length; i++){
    		  h[i] = new OWHub(this.adapter,this.hAddress[i]);
    	  }
    	  activatePath();
    	  deactivatePath();
      }
  }
  
/**
 *  deactivatePath method deactivate hubs on path to 1-wire device
 */
  public void deactivatePath()
  {
	  if(mPath){
		  for(int i = this.h.length-1; i > -1; i--){
			  h[i].deactivateAllPorts();
		  }
	  }
  }
  
/**
 *  activatePath method activate hubs on path to 1-wire device 
 */
  public void activatePath()
  {
      if(mPath){
    	  for(int i = 0; i < this.hAddress.length; i++){
    		  h[i].deactivateAllPorts();
    		  h[i].activatePort(this.hPorts[i]);
    	  }
      }
  }
    
}
